package com.token.iot.bratterycharging.gateway.business;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.api.Constant;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;

/**
 * 设置IC卡和投币器是否可用
 * 
 * @author chenzhixuan
 *
 */

@Component("DCDDuplexProcessor")
public class DCDDuplexProcessor extends DuplexProcessor {

	@Override
	public String request(String imei, Map<String, String> params) throws PendingException {
		String c = "DCD";
		String sessionId = IDGeneration.createSeesionID();
		String toubi = params.get(Constant.COIN);
		String ic = params.get(Constant.CARD);

		String command = null;
		
		command = c + sessionId + "/";

		if ("false".equalsIgnoreCase(toubi)) {
			command =command+"0";
		}else {
			command =command+"1";
		}
		if ("false".equalsIgnoreCase(ic)) {
			command =command+"0";
		}else {
			command =command+"1";
		}
		loger.info("the send =" + new String(command));
		byte[] cc = ByteUtil.addFlag(command.getBytes());
		context.writeAndFlush(cc);
		return sessionId;

	}

	@Override
	public Boolean response(String response) throws PendingException {
		String val = response.substring(14);
		if ("1".equals(val)) {
			return true;
		} else {
			return false;
		}
	}

}
